package cn.edu.zjicm.wordsnet_d.lock;

import android.app.KeyguardManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import cn.edu.zjicm.wordsnet_d.activity.LockActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;

public class LockService extends Service {
	public static void startActivity(Context context) {
		Intent i = new Intent(context, LockService.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startService(i);
	}

	private Intent lockActivityIntent = null;
	private static KeyguardManager mKeyguardManager = null;
	public static KeyguardManager.KeyguardLock mKeyguardLock = null;
	private static final Handler mHandler = new Handler();

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public void onCreate() {
		super.onCreate();
		// LogActivity.addLog("LockService onCreat()");
	}

	public int onStartCommand(Intent intent, int flags, int startId) {
		if (AppPreference.isLockScreen(this))
			disableLock(this);

		registerScreenActionReceiver();/* 注册广播 */
		// LogActivity.addLog("LockService onStartCommand()");
		return Service.START_STICKY;
	}

	public static void disableLock(Context context) {

		if (mKeyguardLock == null) {
			mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
			mKeyguardLock = mKeyguardManager.newKeyguardLock("myNewKeyGuard");
		}
		mKeyguardLock.disableKeyguard();// 屏蔽系统锁屏
		// LogActivity.addLog("屏蔽Lock in disableLock:" +
		// LockService.mKeyguardLock);
		// LogUtil.e("是否加密", mKeyguardManager.isKeyguardSecure() + "");
	}

	public static void unDisableLock(final Context context) {

		if (mKeyguardLock != null)
			mKeyguardLock.reenableKeyguard();
		// LogActivity.addLog("解除屏蔽Lock in disableLock:" +
		// LockService.mKeyguardLock);
	}

	public static void reDisableLock(final Context context) {

		if (mKeyguardManager != null && mKeyguardManager.inKeyguardRestrictedInputMode()) {

		} else {
			if (mKeyguardLock != null)
				mKeyguardLock.reenableKeyguard();

			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					disableLock(context);
				}
			}, 300);
		}
		// LogActivity.addLog("重新屏蔽Lock in disableLock:" +
		// LockService.mKeyguardLock);
	}

	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mScreenReceiver);
		// LogActivity.addLog("LockService onDestroy()");
		this.startService(new Intent(LockService.this, LockService.class));
	}

	/* 注册广播 */
	private void registerScreenActionReceiver() {
		IntentFilter receiverFilter = new IntentFilter();
		receiverFilter.addAction(Intent.ACTION_SCREEN_OFF);
		// receiverFilter.addAction(Intent.ACTION_SCREEN_ON);
		// receiverFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		registerReceiver(mScreenReceiver, receiverFilter);
	}

	private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {

		public void onReceive(Context context, Intent intent) {

			if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
				if (getCallState() == TelephonyManager.CALL_STATE_IDLE) {
					if (!AppPreference.isLockScreen(LockService.this)) // 如果关闭锁屏功能，退出
						return;

					// disableLock(LockService.this); //屏蔽锁屏

					// if
					// (AppPreference.isFirstVisitLockScreen(LockService.this))
					// {
					// lockActivityIntent = new Intent(LockService.this,
					// LockScreenGuideActivity.class);
					// AppPreference.setFirstVisitLockScreen(LockService.this,
					// false);
					// } else {
					lockActivityIntent = new Intent(LockService.this, LockActivity.class);
					// }
					lockActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(lockActivityIntent);

					// LogActivity.addLog("LockService startLockActivity通知activity运行");
				}
			}
		}

		int getCallState() {
			TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
			int callState = telMgr.getCallState();
			switch (callState) {
			case TelephonyManager.CALL_STATE_IDLE:
				Log.e("callstate", "IDLE");
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Log.e("callstate", "OFFHOOK");
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				Log.e("callstate", "RINGING");
				break;
			}
			return callState;
		}
	};

}
